<template>
  <div id="home">
    <!-- 首页导航 -->
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control
      :titles="['流行', '新款', '精选']"
      @tabClick="tabClick"
      ref="tab1"
      class="tab-control"
      v-show="isFixed"
    />
    <scroll
      class="content"
      ref="scroll"
      @scroll="checkScrollPosition"
      :probe-type="3"
      :pull-up-load="true"
      @pullingUp="loadingMore"
    >
      <!-- 轮播 -->
      <home-swiper :banners="banners" @swipperImgLoad="swipperImgLoad" />
      <!-- 推荐 -->
      <HomeRecommend :recommends="recommends" />
      <!-- 本周流行 -->
      <home-feature />

      <tab-control
        :titles="['流行', '新款', '精选']"
        @tabClick="tabClick"
        ref="tab"
      />
      <!-- 商品列表 -->
      <goods-list :goods="showGoods" />
    </scroll>
    <!-- 返回顶部 -->
    <!-- native监听组件内部的原事件 -->
    <back-top @click.native="backClick()" v-show="isShowBackTop" />
  </div>
</template>

<script>
// 首页相关组价
import HomeSwiper from "./childrenComponents/HomeSwiper";
import HomeRecommend from "./childrenComponents/HomeRecommend";
import HomeFeature from "./childrenComponents/HomeFeature";

//公共组件
import NavBar from "components/common/navbar/NavBar";
import TabControl from "components/content/tabcontrol/TabControl";
import GoodsList from "components/content/goods/GoodsList";
import Scroll from "components/common/scroll/Scroll";
import BackTop from "components/content/backTop/BackTop";

import { getHomeMultiData, getHomeGoods } from "network/home";

export default {
  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      currentTab: "pop",
      isShowBackTop: false,
      tabOffsetTop: 0,
      isFixed: false,
      leavingPosition: 0,
    };
  },
  components: {
    HomeSwiper,
    HomeRecommend,
    HomeFeature,
    NavBar,
    TabControl,
    GoodsList,
    Scroll,
    BackTop,
  },
  created() {
    this.getHomeMultiData();
    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
  },
  computed: {
    showGoods() {
      return this.goods[this.currentTab].list;
    },
  },
  activated() {
    this.$refs.scroll.scrollTo(0, this.leavingPosition, 1);
  },
  deactivated() {
    //离开页面记录一下浏览位置
    this.leavingPosition = this.$refs.scroll.getScrollY();
  },
  destroyed() {
    console.log("销毁页面");
  },
  methods: {
    //组件事件相关
    tabClick(index) {
      switch (index) {
        case 0:
          this.currentTab = "pop";
          break;
        case 1:
          this.currentTab = "new";
          break;
        case 2:
          this.currentTab = "sell";
          break;
      }
      this.$refs.tab.currentIndex = index;
      this.$refs.tab1.currentIndex = index;
    },
    backClick() {
      this.$refs.scroll.scrollTo(0, 0, 1000);
    },
    checkScrollPosition(position) {
      //回到顶部
      this.isShowBackTop = -position.y > 1000;
      //tab吸顶距离
      this.isFixed = -position.y > this.tabOffsetTop;
    },
    //加载更多
    loadingMore() {
      this.getHomeGoods(this.currentTab);
    },
    //记录tabControl离顶部的距离
    swipperImgLoad() {
      this.tabOffsetTop = this.$refs.tab.$el.offsetTop;
    },
    //网络请求相关
    getHomeMultiData() {
      getHomeMultiData().then((res) => {
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
      });
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then((res) => {
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page += 1;
        //scroll默认加载一次·
        this.$refs.scroll.finishPullUp();
      });
    },
  },
};
</script>

<style scoped>
#home {
  /* padding-top: 44px; */
  height: 100vh;
  position: relative;
}
.home-nav {
  background-color: var(--color-tint);
  color: var(--color-background);
  /* position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 9; */
}
.tab-control {
  position: relative;
  z-index: 9;
}

.content {
  position: absolute;
  overflow: hidden;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
}
</style>